package com.example.jsonbugdemo;

import cn.hutool.core.util.RandomUtil;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.reactive.function.client.WebClient;
import org.springframework.web.reactive.function.client.support.WebClientAdapter;
import org.springframework.web.service.invoker.HttpServiceProxyFactory;

import java.time.Duration;

/**
 * @description:
 * @author:羽飞
 * @createTime:2023/7/16
 */
@Configuration
public class RestClientConfig {
    private final static String appKey = "--";
    private final static String appSecret = "--";

    @Bean
    public UserRestClient userRestClient() {
        String nonce = RandomUtil.randomString(32);
        String curTime=String.valueOf((System.currentTimeMillis()/1000L));
        String checkSum=CheckSumBuilder.getCheckSum(appSecret,nonce,curTime);

        WebClient client = WebClient.builder()
                .baseUrl("https://api.netease.im")
                .defaultHeaders(httpHeaders->{
                    httpHeaders.add("AppKey", appKey);
                    httpHeaders.add("Nonce", nonce);
                    httpHeaders.add("CurTime", curTime);
                    httpHeaders.add("CheckSum", checkSum);
                    httpHeaders.add("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
                })
                .build();
        HttpServiceProxyFactory factory = HttpServiceProxyFactory.builder(WebClientAdapter.forClient(client))
                .build();
        return factory.createClient(UserRestClient.class);
    }
}
